import STORAGE from '../assets/js/storage.js';
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state = { 
    isLogin: false,
    userInfo: {},
    token: ''
}
const actions = {
    SET_USERINFO ({commit}, userInfo) {
        commit('SET_USERINFO', userInfo)
    }
}
const mutations = {
    SET_USERINFO(state, userInfo){
      if (userInfo) {
        state.userInfo = userInfo
        STORAGE.setStorageState({
          isLogin: true,
          userInfo: userInfo
        })
      } else {
        state.userInfo = {}
        state.isLogin = false
        STORAGE.setStorageState({
          isLogin: false,
          userInfo: {}
        })
      }
    },
    CLEAN_USERINFO(state){
      state.userInfo = {}
      state.isLogin = false
      state.token = ''
      STORAGE.setStorageState({
        isLogin: false,
        userInfo: {},
        token: ''
      })
    },
    SET_TOKEN(state, token){
      state.token = token
      STORAGE.setStorageState({
        token: token
      })
    }
}
const getters = {
  userInfo(state) {
    if (state.userInfo.id) {
      return state.userInfo
    }
    return STORAGE.getStorageKey('userInfo', {})
  },
  token(state) {
    if (state.token) {
      return state.token
    }
    return STORAGE.getStorageKey('token', '')
  }
}
export default new Vuex.Store({
    state,
    actions,
    mutations,
    getters
})